package Offer2018.java8;

import Offer2018.java8.Employee;
import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.*;

public class MethodRefTest {


    //1.对象::实例方法
    //Consumer中的void accept(T t)
    //PrintStream中的void println(Object x)
    @Test
    public void test1() {

        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String str) {
                System.out.println(str);
            }
        };
        consumer.accept("世界");

        //lambda简写
        Consumer con1 = str -> System.out.println(str);
        con1.accept("你好");

        //方法引用使用PrintStream中的void println(T t)
        PrintStream printStream = System.out;
        Consumer tConsumer1 = printStream::println;
        tConsumer1.accept("世界你好");

        Consumer tConsumer = System.out::print;
        tConsumer.accept("你好世界");
    }

    //Supplier中的T get()
    //Employee中的String getName()
    @Test
    public void test2() {
        Employee common = new Employee(10000, "common", 1, 0);
        Supplier<String> s1 = new Supplier<String>() {
            @Override
            public String get() {
                return common.getName();
            }
        };
        System.out.println(s1.get());

        //lambda表达式
        Employee kylin = new Employee(10001, "kylin", 11, 1111);
        Supplier<String> s2 = () -> kylin.getName();
        System.out.println(s2.get());

        //方法引用
        Employee mr = new Employee(10001, "MethodRef", 7, 9999);
        Supplier<String> s3 = mr::getName;
        System.out.println(s3.get());
    }

    //2.类::静态方法
    //Comparator中的int compare(T t1,T t2)
    //Integer中的int compare(T t1,T t2)
    @Test
    public void test3() {
        //普通
        Comparator<Integer> c1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer t1, Integer t2) {

                return Integer.compare(t1, t2);
            }
        };
        System.out.println(c1.compare(12, 21));

        //lambda
        Comparator<Integer> c2 = (t1, t2) -> Integer.compare(t1, t2);
        System.out.println(c2.compare(12, 21));


        //方法引用，参数一样省略
        Comparator<Integer> c3 = Integer::compare;
        System.out.println(c3.compare(12, 21));
    }

    //Function中的R apply(T t)
    //Math中的Long round(Double d)
    @Test
    public void test4() {
        //普通
        Function<Double, Long> function1 = new Function<Double, Long>() {

            @Override
            public Long apply(Double d) {
                return Math.round(d);
            }
        };
        System.out.println(function1.apply(5.3));

        //lambda
        Function<Double, Long> function2 = (d) -> Math.round(d);
        System.out.println(function2.apply(5.4));

        //方法引用
        Function<Double, Long> function3 = Math::round;
        System.out.println(function3.apply(5.5));
    }


    //3.类::实例方法
    //Comparator中的int compare(T t1,T t2)
    //String中的int compareTo(String s)---->t1.compareTo(t2)
    @Test
    public void test5() {
        //普通方法
        Comparator<String> c1 = new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareTo(s2);
            }
        };

        System.out.println(c1.compare("1", "4"));
        //lambda
        Comparator<String> c2 = (s1, s2) -> s1.compareTo(s2);
        System.out.println(c2.compare("3", "7"));

        //方法引用
        Comparator<String> c3 = String::compareTo;
        System.out.println(c3.compare("9", "1"));

    }

    //BiPredicate中的boolean test(T t1,T t2);
    //String中的boolean equals(O o)---->t1.equals(t2)
    @Test
    public void test6() {
        //普通方法
        BiPredicate<String, String> b1 = new BiPredicate<String, String>() {
            @Override
            public boolean test(String s1, String s2) {
                return s1.equals(s2);
            }
        };
        System.out.println(b1.test("a", "a"));

        //lambda
        BiPredicate<String, String> b2 = (s1, s2) -> s1.equals(s2);
        System.out.println(b2.test("c", "a"));

        //方法引用
        BiPredicate<String, String> b3 = String::equals;
        System.out.println(b3.test("e", "e"));
    }



    //Function中的R apply(T t)
    //Employee中的String getName();--->t.getName();
    @Test
    public void test7() {
        //普通形式
        Employee kylin = new Employee(10001, "kylin", 11, 1111);
        Function<Employee, String> f1 = new Function<Employee, String>() {
            @Override
            public String apply(Employee employee) {
                return employee.getName();
            }
        };
        System.out.println(f1.apply(kylin));

        //lambda
        Function<Employee, String> f2 = employee -> employee.getName();
        System.out.println(f2.apply(kylin));

        //方法引用
        Function<Employee, String> f3 = Employee::getName;
        System.out.println(f3.apply(kylin));
    }
}


